package package02;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 作者: jack
 * 时间: 2020-07-23 15:49
 * 描述: Test
 * 多线程情况下使用队列
 */
public class Test {

//    public static void main(String[] args) {
//
//        LinkedList<Integer> list = new LinkedList<>();
//        list.add(100);
//
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Integer remove = list.remove();
//                System.out.println(remove);
//            }
//        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                Integer remove = list.remove();
//                System.out.println(remove);
//            }
//        }).start();
//
//    }

    public static void main(String[] args) throws Exception {

        Queue<Integer> queue = new LinkedBlockingDeque<>(1);
        queue.add(100);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Integer number = null;
                do {
                    number = queue.poll();
                } while (number == null);
                System.out.println(number);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Integer number = null;
                do {
                    number = queue.poll();
                } while (number == null);
                System.out.println(number);
            }
        }).start();


        Thread.sleep(3000);
        // 又排队
        queue.offer(200);
    }

}
